package com.yc.api2;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/11/3
 * Email haijunzhou@hnit.edu.cn
 */
public class MyCollection {

    public static void main(String[] args) {
        List<Integer> list = new MyLinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(1);
        list.add(2);

        System.out.println("list.size() = " + list.size());
        System.out.println("list.get(1) = " + list.get(1));
        System.out.println("list.get(3) = " + list.get(3));

        for (Integer i : list) {
            System.out.println("i = " + i);
        }
    }

    public static void iterator(String[] args) {
        Integer[] arr = {1,2,3,4,5,6,7};
        MyArrayList list = new MyArrayList(arr);

        System.out.println("============ 增强for循序 =============");
        for (Integer i : list) {
            System.out.println("i = " + i);
        }

        System.out.println("============ 迭代器 while 循序 =============");
        Iterator<Integer> it = list.iterator();
        while(it.hasNext()){
            int i = it.next();
            System.out.println("i = " + i);
        }
        // 注意: 迭代器是一个只能向前走的对象, 一旦迭代完成, 就不能继续使用了
        System.out.println("it.hasNext() = " + it.hasNext());
        //System.out.println("it.next() = " + it.next());

        System.out.println("============ 迭代器 for 循序 =============");

        for( Iterator<Integer> itt = list.iterator() ; itt.hasNext() ; ){
            int i = itt.next();
            System.out.println("i = " + i);
        }

    }
}

// 基于数组实现
class  MyArrayList implements Iterable<Integer>{

    Integer[] array;

    public MyArrayList(Integer[] array) {
        this.array = array;
    }

    @Override
    public Iterator<Integer> iterator() {
        // 匿名类对象
        Iterator<Integer> it = new Iterator<Integer>() {
            int index = 0;
            @Override
            public boolean hasNext() {
                return index < array.length;
            }

            @Override
            public Integer next() {
                // 返回下一个元素, 同时将指针+1
                return array[index++];
            }
        };

        return it;
    }
}

// 基于链表实现
class MyLinkedList<T> implements List<T>{

    class Node<T> {
        // 单链表
        T data;  // 数值域
        Node<T> next; // 指针域
    }

    // 单链表
    Node<T> head;

    @Override
    public int size() {
        int size = 0;
        Node<T> node = head;
        while(node!=null){
            size ++;
            node = node.next;
        }
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size() == 0;
    }

    // 包含
    @Override
    public boolean contains(Object o) {
        Node<T> node = head;
        while(node!=null){
            if(node.data.equals(o)){
                return true;
            }
            node = node.next;
        }
        return false;
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {
            Node<T> node = head;
            @Override
            public boolean hasNext() {
                return node != null;
            }

            @Override
            public T next() {
                Node<T> currentNode = node;
                node = node.next;
                return currentNode.data;
            }
        };
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public boolean add(T t) {
        if(head == null){
            head = new Node<>();
            head.data = t;
            return true;
        }
        Node<T> node = head;
        Node<T> preNode = head;
        while(node != null){
            preNode = node;
            node = node.next;
        }
        node = new Node<>();
        node.data = t;
        preNode.next = node;
        return true;
    }

    @Override
    public T get(int index) {
        Node<T> node = head;
        int i = 0;
        while(node!=null){
            if(i++ == index){
                return node.data;
            }
            node = node.next;
        }
        throw new ArrayIndexOutOfBoundsException(index);
    }

    @Override
    public <T1> T1[] toArray(T1[] a) {
        return null;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends T> c) {
        return false;
    }

    @Override
    public boolean addAll(int index, Collection<? extends T> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public T set(int index, T element) {
        return null;
    }

    @Override
    public void add(int index, T element) {

    }

    @Override
    public T remove(int index) {
        return null;
    }

    @Override
    public int indexOf(Object o) {
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @Override
    public ListIterator<T> listIterator() {
        return null;
    }

    @Override
    public ListIterator<T> listIterator(int index) {
        return null;
    }

    @Override
    public List<T> subList(int fromIndex, int toIndex) {
        return null;
    }
}

